package com.rocching.easychat.service;

import android.annotation.SuppressLint;

import com.rocching.easychat.core.IApiCallback;
import com.rocching.easychat.core.UserIdentityContext;
import com.rocching.easychat.model.FriendListModel;
import com.rocching.easychat.model.UserModel;
import com.rocching.easychat.model.UserPhotoModel;
import com.rocching.easychat.model.UserTokenModel;
import com.rocching.easychat.service.dto.LoginInput;
import com.rocching.easychat.service.dto.RegisterUserInput;
import com.rocching.easychat.service.dto.UpdateUserInput;
import com.rocching.easychat.util.HttpUtil;

import java.io.File;
import java.util.Objects;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class UserService {
    private static UserService instance;

    public static UserService getInstance() {
        if (Objects.isNull(instance)) {
            instance = new UserService();
        }
        return instance;
    }

    public void login(LoginInput input, IApiCallback<UserModel> callback) {
        HttpUtil.post("/api/RBAC/Login", input, callback, UserModel.class);
    }

    public void register(RegisterUserInput input, IApiCallback<UserModel> callback) {
        HttpUtil.post("/api/RBAC/Register", input, callback, UserModel.class);
    }

    public void getToken(String account, IApiCallback<UserTokenModel> callback) {
        HttpUtil.get(String.format("/api/RCUser/GetToken/%s", account), callback, UserTokenModel.class);
    }

    public void uploadPhoto(File file, IApiCallback<UserPhotoModel> callback) {
        RequestBody fileBody = RequestBody.create(file, MediaType.parse("image/png"));
        MultipartBody body = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", file.getName(), fileBody).build();
        HttpUtil.uploadFile("/api/RCUser/UploadFile", body, callback, UserPhotoModel.class);
    }

    public void updateUser(UpdateUserInput input, IApiCallback<UserModel> callback) {
        HttpUtil.put("/api/RCUser/UpdateUser", input, callback, UserModel.class);
    }
}
